package com.hzhk.happyrent.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Base64;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;


public class SystemUtils {


    public static final String MOBILE_TESTTWO = "^((13[0-9])|(15[0-9])|(17[0-9])|(18[0-9])|(14[0-9]))\\d{8}$";
    public static final Pattern MOBILE_PATTERN = Pattern
            .compile(MOBILE_TESTTWO);

    public static boolean isMobileNO(String mobiles) {
        if (!TextUtils.isEmpty(mobiles)) {
            if (mobiles.startsWith("1") && mobiles.length() == 11) {
                return true;
            } else {
                return false;
            }
//			return MOBILE_PATTERN.matcher(mobiles).matches();
        } else {
            return false;
        }
    }




    /**
     * 设置页面背景透明度
     *
     * @param alpha =1.0f透明
     * @param
     * @return
     */
    public static void setBackAlpha(Activity activity, float alpha) {
        LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = alpha;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//张志峰
        activity.getWindow().setAttributes(lp);
    }

    /**
     * 打开浏览器
     *
     * @param alpha =1.0f透明
     * @param
     * @return
     */
    public static void openBrowser(Context context, String url) {
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        context.startActivity(intent);
    }

    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        for (RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                /*
                 * BACKGROUND=400 EMPTY=500 FOREGROUND=100 GONE=1000
				 * PERCEPTIBLE=130 SERVICE=300 ISIBLE=200
				 */
                /*
				 * Log.i(context.getPackageName(), "此appimportace =" +
				 * appProcess.importance + ",context.getClass().getName()=" +
				 * context.getClass().getName());
				 */
                if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {//处于后台
                    return true;
                } else {//处于前台
                    return false;
                }
            }
        }
        return false;
    }

    public static boolean isLetterDigit(String str) {
        boolean isDigit = false;// 定义一个boolean值，用来表示是否包含数字
        boolean isLetter = false;// 定义一个boolean值，用来表示是否包含字母
        for (int i = 0; i < str.length(); i++) {
            if (Character.isDigit(str.charAt(i))) { // 用char包装类中的判断数字的方法判断每一个字符
                isDigit = true;
            } else if (Character.isLetter(str.charAt(i))) { // 用char包装类中的判断字母的方法判断每一个字符
                isLetter = true;
            }
        }
        //String reguler="^[a-zA-Z0-9_]*$";//匹配只由数字、26个英文字母或者下划线组成的字符串
        String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
        boolean isRight = isDigit && isLetter && str.matches(regex);
        return isRight;
    }

    public static void showAlertDialog(Context mContext, String title, String message, String pButton, String nButton, DialogInterface.OnClickListener pListener,
                                       DialogInterface.OnClickListener nListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle(title);
        builder.setMessage(message);
        pButton = pButton == null ? "确定" : pButton;
        if (pListener != null) {
            builder.setPositiveButton(pButton, pListener);
        }
        nButton = nButton == null ? "取消" : nButton;
        if (nListener != null) {
            builder.setNegativeButton(nButton, nListener);
        }
        Dialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }
    /**
     * bitmap转为base64
     * @param bitmap
     * @return
     */
    public static String bitmapToBase64(Bitmap bitmap) {

        String result = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

                baos.flush();
                baos.close();

                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
